$(function() {  

  // body.onclick = function() {
  //   console.log(1);
  //   $('.mask').css('opacity', 1);
  // }
  // 隐藏loading效果
  onload = function() {
    $('.loading-box').css('display', 'none')
  }
  /**
   * 请求首页轮播数据
   */
  $.ajax({
    type: 'get',
    url: 'https://supergeek.api.likedata.cn/activity/cms/index',
    async: false,
    success(res) {
      // console.log(res);
      if (res.success) {
        
        var html = template('template', res);
        
        $('.js-carousel').html(html);

        // console.log(html)
      }
    }
  })  

  /**
   * 初始化轮播图
   */
  let res = $(".js-carousel").carousel({
    visibleCount: 30,
    step: .2,
    // rotateY: 10
  });
  

  

  /**
   * 点击每一项根据返回数据判断去往列表页或详情页
   */
  $('.js-carousel li').on('click', function() {
    // console.log(this.dataset.id)
    $.ajax({
      type: 'get',
      url: 'https://supergeek.api.likedata.cn/activity/cms/detail?categoryId=' + this.dataset['id'],
      success: (res) => {
        // console.log(res);
        if (res.data.length == 0) {
          $('.mask').css({'opacity': 1, 'z-index': 1000});
          setTimeout(function() {
            $('.mask').css({'opacity': 0, 'z-index': 0});
          }, 1000)
          return false
        };

        if (res.data.length > 1) {
          location.href = './list.html?id=' + this.dataset['id'];
        } else {
          location.href = './detail.html?id=' + this.dataset['id'];
        }

      }
    })
  })

})